package com.antt.cn.test04.views;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Path;
import android.util.AttributeSet;
import android.view.MotionEvent;

/**
 * Created by Ant on 2017/5/26  16:07
 * <p>
 * <p>
 *
 * @version 1.0.0
 * @auther Ant
 */

public class LimitLinesTextView extends android.support.v7.widget.AppCompatTextView {
    private int lineLimit = 3;
    private boolean lineLimited = true;
    private Paint paint;
    private Path path1, path2;

    public LimitLinesTextView(Context context) {
        super(context);
        initThisView(context, null, 0);
    }

    public LimitLinesTextView(Context context, AttributeSet attrs) {
        super(context, attrs);
        initThisView(context, attrs, 0);
    }

    public LimitLinesTextView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initThisView(context, attrs, defStyleAttr);
    }


    @SuppressLint("NewApi")
    private void initThisView(Context context, AttributeSet attrs, int defStyleAttr) {
        // TODO some init things  ^_^
        lineLimit = Math.max(lineLimit, getMaxLines());
        setMaxLines(lineLimit);
        paint = new Paint();
        paint.setAntiAlias(true);
        paint.setColor(getCurrentTextColor());
        paint.setStyle(Paint.Style.FILL);

        path1 = new Path();
        path1.moveTo(0, 0);
        path1.lineTo(30, 0);
        path1.lineTo(15, 15);
        path1.close();
        path2 = new Path();
        path2.moveTo(0, 0);
        path2.lineTo(15, 0);
        path2.lineTo(30, 30);
        path2.close();
    }

    @Override
    protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
        super.onLayout(changed, left, top, right, bottom);

    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        toDrawRectTangle(canvas);
    }

    private void toDrawRectTangle(Canvas canvas) {

        canvas.save();
        canvas.translate(getRight() - 200, getBottom() - 200);
        if (lineLimited) {
            canvas.drawPath(path1, paint);
        } else {
            canvas.drawPath(path2, paint);
        }
        canvas.drawRect(0, 0, 20, 20, paint);
        canvas.drawText("askldjfaksl",0,0,paint);
        canvas.restore();
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        switch (event.getAction()) {
            case MotionEvent.ACTION_UP:
                fingerUp();
                return true;
        }
        return super.onTouchEvent(event);
    }

    private void fingerUp() {
        if (lineLimited) {
            setMaxLines(Integer.MAX_VALUE >> 1);
            lineLimited = false;
        } else {
            setMaxLines(lineLimit);
            lineLimited = true;
        }
    }
}
